package alp.starcode.sanping.dataapply.service;

import alp.starcode.common.base.exception.OperateException;
import alp.starcode.common.base.exception.ParamsNullException;
import alp.starcode.common.mybatis.page.Pagination;
import alp.starcode.common.mybatis.sort.Sorter;
import alp.starcode.common.mybatis.util.PageBeanUtil;
import alp.starcode.sanping.dataapply.model.dto.DataApplyDTO;
import alp.starcode.sanping.dataapply.model.vo.DataApplyVO;
import alp.starcode.sanping.framework.base.BaseService;
import alp.starcode.sanping.framework.database.mariadb.mybatis.dao.DataApplyDao;
import alp.starcode.sanping.framework.database.mariadb.mybatis.entity.DataApply;
import alp.starcode.sanping.information.model.vo.InformationVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * TODO
 *
 * @author wujie
 * @date 2024/3/14 17:32
 */
@Service
public class DataApplyService extends BaseService {

    @Autowired
    DataApplyDao dataApplyDao;

    /**
     * 分页查询信息
     *
     * @param pagination    分页对象
     * @param searchKeyword 搜索关键字
     * @param sorters       排序字段
     * @return 分页对象
     */
    public Pagination<DataApplyVO> pageList(Pagination<DataApply> pagination, String searchKeyword, Sorter sorters) {
        MPJLambdaWrapper<DataApply> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(DataApply.class);
        // 搜索关键字
        if (StringUtils.isNotEmpty(searchKeyword)) {
            queryWrapper.and(qw -> qw.or().like(DataApply::getLinkMan, searchKeyword)
                    .or().like(DataApply::getCity, searchKeyword));
        }
        // 有效数据
        queryWrapper.eq(DataApply::getIsDeleted, Boolean.FALSE);
        //构建排序条件
        sorters.sort(queryWrapper);
        //分页查询数据，并构建返回对象
        return PageBeanUtil.copyProperties(dataApplyDao.page(pagination, queryWrapper), DataApplyVO.class);
    }

    /**
     * 根据Id获取信息
     *
     * @param id 主键Id
     * @return Role
     */
    public DataApplyVO getById(String id) {
        if (id == null || id.isEmpty()) {
            throw new ParamsNullException("[id]不能为空！");
        }
        MPJLambdaWrapper<DataApply> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(DataApply.class);
        queryWrapper.eq(DataApply::getId, id);
        // 有效数据
        queryWrapper.eq(DataApply::getIsDeleted, Boolean.FALSE);

        DataApply entity = dataApplyDao.getOne(queryWrapper);
        return BeanUtil.copyProperties(entity, DataApplyVO.class);
    }

    /**
     * 添加数据
     *
     * @param entityDTO 参数对象
     */
    public void add(DataApplyDTO entityDTO) {
        // 进行数据转换
        DataApply entity = entityDTO.converter();
        // 生成流水号
        String uuid = UUID.randomUUID().toString();
        entity.setId(uuid);
        entity.setIsDeleted(Boolean.FALSE);
        // 设置提交时间
        entity.setSubmitTime(System.currentTimeMillis());
        // 判断数据是否新增成功，失败抛出异常提示。
        if (!dataApplyDao.save(entity)) {
            throw new OperateException("数据添加失败，写入数据异常：[%s]", entity);
        }
    }

    /**
     * 删除数据
     *
     * @param entityDTO 参数对象
     */
    public void delete(DataApplyDTO entityDTO) {
        DataApply entity = new DataApply();
        entity.setId(entityDTO.getId());
        entity.setIsDeleted(Boolean.TRUE);
        if (!dataApplyDao.updateById(entity)) {
            throw new OperateException("数据删除失败，写入数据异常：[%s]", entity);
        }
    }

    /**
     * 修改数据
     *
     * @param entityDTO 参数对象
     */
    public void update(DataApplyDTO entityDTO) {
        DataApply entity = entityDTO.converter();
        //角色KEY不允许修改，避免产生脏数据
        if (!dataApplyDao.updateById(entity)) {
            throw new OperateException("数据修改失败，写入数据异常：[%s]", entity);
        }
    }

}
